﻿using System;
using System.Windows;
using Kiva7.Utilities;
using Kiva7.DataClasses;
using Kiva7.DataRequests;
using Kiva7.Images;
using Kiva7.Translations;

namespace Kiva7.Pages
{
    public partial class SearchResultPage
    {
        private const string Url = "/Pages/SearchResultPage.xaml";
        public readonly static Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        private const string Query = "query";
        private const string Status = "status";
        private const string Gender = "gender";
        private const string Region = "region";
        private const string CountryCode = "countryCode";
        private const string Sector = "sector";
        private const string PartnerId = "partnerId";
        private const string SortBy = "sortBy";
        private const string HasCurrencyLoss = "hasCurrencyLoss";

        public static Uri GetPageUrl(
            string query,
            string status,
            Gender? gender,
            string region,
            string countryCode,
            string sector,
            int? partnerId,
            string sortBy,
            bool? hasCurrencyLoss)
        {
            UrlCreator url = new UrlCreator(Url);
            
            url.Add(Query, query);
            url.Add(Status, status);
            url.Add(Gender, gender);
            url.Add(Region, region);
            url.Add(CountryCode, countryCode);
            url.Add(Sector, sector);
            url.Add(PartnerId, partnerId);
            url.Add(SortBy, sortBy);
            url.Add(HasCurrencyLoss, hasCurrencyLoss);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }

        public SearchResultPage()
        {
            InitializeComponent();
            CreateStandardControls();
        }

        LoanSearchRequest loanRequest;
        private void RequestLoans()
        {
            if (loanRequest == null)
            {
                loanRequest = new LoanSearchRequest();
                LoansControl.DataContext = loanRequest;
            }

            string query = NavigationContext.GetQueryString(Query);
            string status = NavigationContext.GetQueryString(Status);
            string gender = NavigationContext.GetQueryString(Gender);
            string region = NavigationContext.GetQueryString(Region);
            string countryCode = NavigationContext.GetQueryString(CountryCode);
            string sector = NavigationContext.GetQueryString(Sector);
            string partnerId = NavigationContext.GetQueryString(PartnerId);
            string sortBy = NavigationContext.GetQueryString(SortBy);
            string hasCurrencyLoss = NavigationContext.GetQueryString(HasCurrencyLoss);


            loanRequest.GetLoans(query,
                status,
                gender,
                region,
                countryCode,
                sector,
                partnerId,
                sortBy,
                hasCurrencyLoss);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            RequestLoans();
        }


        private void SendBasket_Click(object sender, EventArgs e)
        {
            if (!Baskets.Basket.Current.IsEmpty)
            {
                DoNavigate(BasketPage.PageUrl);
            }
        }

        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.SendBasket, ImageUrls.SendBasket, new EventHandler(SendBasket_Click));
        }

    }
}